import axios from 'axios';

export function getLatestHumidity() {

     var host = window.location.hostname;
    var config = {
        method: 'get',
        url: 'http://'+host+':8080/humidity/latest/1',
        headers: {}
    };

    return new Promise((resolve, reject) => {
        axios(config)
            .then(function (response) {
                const value = response.data.data[0].value;
                resolve(value);
            })
            .catch(function (error) {
                reject(error);
            });
    });
}

export function getLatestTemperature() {

    var host = window.location.hostname;
    var config = {
        method: 'get',
        url: 'http://'+host+':8080/temperature/latest/1',
        headers: {}
    };

    return new Promise((resolve, reject) => {
        axios(config)
            .then(function (response) {
                const value = response.data.data[0].value;
                resolve(value);
            })
            .catch(function (error) {
                reject(error);
            });
    });
}

export function getLatestWaterLevel() {

    var host = window.location.hostname;
    var config = {
        method: 'get',
        url: 'http://'+host+':8080/waterlevel/latest/1',
        headers: {}
    };

    return new Promise((resolve, reject) => {
        axios(config)
            .then(function (response) {
                const value = response.data.data[0].value;
                resolve(value);
            })
            .catch(function (error) {
                reject(error);
            });
    });
}

export function getLatestAcceleration() {
    
    var host = window.location.hostname;
    var config = {
        method: 'get',
        url: 'http://'+host+':8080/acceleration/latest/1',
        headers: {}
    };

    return new Promise((resolve, reject) => {
        axios(config)
            .then(function (response) {
                console.log(response.data.data[0]);
                const x = response.data.data[0].x;
                const y = response.data.data[0].y;
                const z = response.data.data[0].z;
                resolve([x, y, z]);
            })
            .catch(function (error) {
                reject(error);
            });
    });
}

